/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.dialogs;

import com.google.inject.Inject;
import listo.client.ImageProvider;
import listo.client.MainForm;
import listo.client.dialogs.controllers.FolderIconController;
import listo.client.dialogs.controllers.FolderNameController;
import listo.client.model.Folder;
import listo.utils.reflection.Reflector;

import javax.swing.*;
import java.awt.*;

public class FolderDialog extends BaseDialog<Folder> {

    private final FolderIconController iconController;
    private final FolderNameController nameController;

    @Inject
    public FolderDialog(MainForm mainForm, ImageProvider imageProvider, Reflector reflector,
                        FolderIconController iconController, FolderNameController nameController) {
        super(mainForm, imageProvider, reflector);
        this.iconController = iconController;
        this.nameController = nameController;
    }

    protected void createUI() {
        super.createUI();

        JComboBox comboBoxIcon = new JComboBox();
        JLabel labelIcon = new JLabel("Icon");
        labelIcon.setLabelFor(comboBoxIcon);
        comboBoxIcon.setModel(new DefaultComboBoxModel(Folder.getIcons()));
        comboBoxIcon.setRenderer(new ComboBoxRenderer());
        attach(comboBoxIcon, iconController);

        JTextField textFieldName = new JTextField();
        JLabel labelName = new JLabel("Folder Name");
        labelName.setLabelFor(textFieldName);
        attach(textFieldName, nameController);

        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.TRAILING).
                        addGroup(layout.createSequentialGroup().
                                addGroup(layout.createParallelGroup().
                                        addComponent(labelIcon).
                                        addComponent(comboBoxIcon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                        GroupLayout.PREFERRED_SIZE)).
                                addGap(5).
                                addGroup(layout.createParallelGroup().
                                addComponent(labelName).
                                addComponent(textFieldName, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))).
                        addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().
                        addComponent(buttonOk).
                        addComponent(buttonCancel)));
        layout.setVerticalGroup(
                layout.createSequentialGroup().
                        addGroup(layout.createParallelGroup().
                                addComponent(labelIcon).
                                addComponent(labelName)).
                        addGroup(layout.createParallelGroup().
                                addComponent(comboBoxIcon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                        GroupLayout.PREFERRED_SIZE).
                                addComponent(textFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE)).
                        addGap(5).
                        addGroup(layout.createParallelGroup().
                        addComponent(buttonOk).
                        addComponent(buttonCancel)));

        layout.linkSize(SwingConstants.HORIZONTAL, buttonOk, buttonCancel);

        setContentPane(contentPane);
        firstFocusComponent = textFieldName;
    }

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
        @SuppressWarnings({"unchecked"})
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                                                      boolean cellHasFocus) {
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }

            String text = (String) value;
            setText(text);
            setIcon(imageProvider.getIcon("16x16/folders/" + Folder.getIcon(text)));
            setOpaque(true);
            return this;
        }
    }
}
